home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / Reference / DevCon / Orlando_1993 / Devcon93.4 / CAMD / examples / vu / vu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-12  |  5.6 KB  |  246 lines

  1.  
  2. #include <intuition/intuition.h>
  3.  
  4. #include <exec/interrupts.h>
  5. #include <hardware/intbits.h>
  6. #include <midi/camd.h>
  7. #include <midi/mididefs.h>
  8. #include <clib/camd_protos.h>
  9. #include <pragmas/camd_pragmas.h>
  10.  
  11. #include <dos/dos.h>
  12.  
  13. #define BYNOTE            1
  14. #define VELSCALE        150
  15.  
  16. #define    LEAVE            goto exitit;
  17. #define    unless(x)        if ( !(x) )
  18.  
  19. struct Library            *IntuitionBase,
  20.                         *GfxBase,
  21.                         *CamdBase;
  22. extern struct Library    *SysBase;
  23.  
  24. struct Window            *Window;
  25. struct Screen            *Screen;
  26. struct ViewPort            *vp;
  27. struct RastPort            *rp;
  28.  
  29. USHORT colors[32] = {
  30.     0x000,0xAAA,0x888,0x555,0x333,0xBB0,0x6B0,0x0F0,
  31.     0xF99,0xF89,0xE8A,0xE8A,0xE7A,0xE7B,0xD6B,0xD6C,
  32.     0xD5D,0xC5D,0xA5C,0x94C,0x84C,0x74B,0x53B,0x43B,
  33.     0x333,0x444,0x555,0x666,0x777,0x888,0xEEE,0xFFF };
  34.  
  35. struct NewScreen NewScreen = { 0,200-47,320,47, 5, 0,1, NULL, CUSTOMSCREEN, };
  36.  
  37. extern struct Image        im1,
  38.                         im2,
  39.                         im3;
  40.  
  41. struct Gadget off_gadget = {
  42.     NULL,    2,2,13,21,GFLG_GADGHIMAGE|GFLG_GADGIMAGE,GACT_RELVERIFY,GTYP_BOOLGADGET,(APTR)&im1,(APTR)&im2
  43. };
  44.  
  45. struct NewWindow back_ground =
  46. {    0,0,320,47, 0,1,
  47.     VANILLAKEY | MOUSEBUTTONS | GADGETUP,
  48.     NOCAREREFRESH | ACTIVATE | BORDERLESS | RMBTRAP,
  49.     &off_gadget,NULL,NULL,NULL,NULL,320,200,320,200,CUSTOMSCREEN,
  50. };
  51.  
  52. #if BYNOTE
  53. #define VU_BARCOUNT        32
  54. #define BARS_PER_COLOR    2
  55. #else
  56. #define VU_BARCOUNT        16
  57. #define BARS_PER_COLOR    1
  58. #endif
  59.  
  60. WORD                    vu_levels[VU_BARCOUNT],
  61.                         prev_levels[VU_BARCOUNT];
  62.  
  63. struct Task                *myTask;
  64.  
  65. void draw_vu_levels(void);
  66.  
  67. int __interrupt __saveds VBlankInterface( void );
  68.  
  69. struct Interrupt VertBlank =
  70. {    NULL,NULL,NT_INTERRUPT,-60,"VU VBlank",        /* node, pri = -60 */
  71.     NULL,                                        /* data ptr, same as inputevent */
  72.     (void *)VBlankInterface                        /* code ptr */
  73. };
  74.  
  75. WORD                    vblank_ok=NULL;            /* TRUE if vblank int installed    */
  76.  
  77. struct MidiNode            *midi;
  78. struct MidiLink            *link;
  79.  
  80. struct MidiNode *CreateMidi(Tag tag, ...)
  81. {    return CreateMidiA((struct TagItem *)&tag );
  82. }
  83.  
  84. BOOL SetMidiAttrs(struct MidiNode *mi, Tag tag, ...)
  85. {    return SetMidiAttrsA(mi, (struct TagItem *)&tag );
  86. }
  87.  
  88. struct MidiLink *AddMidiLink(struct MidiNode *mi, LONG type, Tag tag, ...)
  89. {    return AddMidiLinkA(mi, type, (struct TagItem *)&tag );
  90. }
  91.  
  92. BOOL SetMidiLinkAttrs(struct MidiLink *mi, Tag tag, ...)
  93. {    return SetMidiLinkAttrsA(mi, (struct TagItem *)&tag );
  94. }
  95.  
  96. void main(int argc,char *argv[])
  97. {    char    *inlinkname = "in.0";
  98.  
  99.     if (argc > 1) inlinkname = argv[1];
  100.  
  101.     myTask = FindTask(0);
  102.  
  103.     unless (IntuitionBase = OpenLibrary("intuition.library",0)) LEAVE;
  104.     unless (GfxBase = OpenLibrary("graphics.library",0)) LEAVE;
  105.     unless (CamdBase = OpenLibrary("camd.library",0)) LEAVE;
  106.  
  107.     if ((Screen = OpenScreen(&NewScreen)) == NULL) LEAVE;
  108.     vp = &(Screen->ViewPort);
  109.     LoadRGB4(vp,colors,32);
  110.  
  111.     back_ground.Screen = Screen;
  112.     if ((Window = OpenWindow(&back_ground)) == NULL) LEAVE;
  113.     rp = Window->RPort;
  114.  
  115.     DrawImage(rp,&im3,309,0);
  116.     SetAPen(rp, 1); Move(rp,19, 0); Draw(rp,308, 0);
  117.     SetAPen(rp,24); Move(rp,19, 1); Draw(rp,308, 1);
  118.     SetAPen(rp, 3); Move(rp,19, 2); Draw(rp,308, 2);
  119.  
  120.     SetAPen(rp, 1); Move(rp,19,44); Draw(rp,308,44);
  121.     SetAPen(rp,24); Move(rp,19,45); Draw(rp,308,45);
  122.     SetAPen(rp, 3); Move(rp,19,46); Draw(rp,308,46);
  123.  
  124.     unless (midi = CreateMidi(
  125.         MIDI_Name, "VU Meters",
  126.         MIDI_RecvSignal, SIGBREAKB_CTRL_E,
  127.         MIDI_MsgQueue,   100,
  128.         MIDI_ErrFilter, CMEF_All,
  129.         TAG_DONE))
  130.             LEAVE;
  131.  
  132.     unless (link = AddMidiLink(midi, MLTYPE_Receiver,
  133.         MLINK_Name, "VU Meter Link",
  134.         MLINK_Location,    inlinkname,
  135.         MLINK_EventMask, CMF_Note,
  136.         MLINK_Comment,    "VU Meters [Input]",
  137.         TAG_DONE))
  138.             LEAVE;
  139.  
  140.     AddIntServer(INTB_VERTB, &VertBlank);
  141.     vblank_ok = TRUE;
  142.  
  143.     while (TRUE)
  144.     {    struct IntuiMessage *message;
  145.         LONG            signals;
  146.  
  147.         signals = Wait((1 << Window->UserPort->mp_SigBit) | SIGBREAKF_CTRL_F | SIGBREAKF_CTRL_E);
  148.  
  149.         if (signals & SIGBREAKF_CTRL_F)
  150.         {    int i;
  151.  
  152.             for (i=0; i<VU_BARCOUNT; i++)
  153.             {    if (vu_levels[i] > 0)
  154.                     vu_levels[i]--;
  155.             }
  156.             draw_vu_levels();
  157.         }
  158.  
  159.         if (signals & SIGBREAKF_CTRL_E)
  160.         {
  161.             MidiMsg        msg;
  162.  
  163.             while (GetMidi(midi,&msg))
  164.             {
  165.                 if (noteon(&msg))
  166. #if BYNOTE
  167.                 {    int        note = ((msg.mm_Data1-36)/2) % 31;
  168.                     int        velocity = msg.mm_Data2 * 39 / 127;
  169.  
  170.                     velocity = velocity * VELSCALE / 100;
  171.                     if (velocity > 39) velocity = 39;
  172.  
  173.                     if (vu_levels[note] < velocity) vu_levels[note] = velocity;
  174.                 }
  175. #else
  176.                 {    int        channel = msg.mm_Status & 0x0f;
  177.                     int        velocity = msg.mm_Data2 * 39 / 127;
  178.  
  179.                     velocity = velocity * VELSCALE / 100;
  180.                     if (velocity > 39) velocity = 39;
  181.  
  182.                     if (vu_levels[channel] < velocity) vu_levels[channel] = velocity;
  183.                 }
  184. #endif
  185.             }
  186.         }
  187.  
  188.         while (message = (struct IntuiMessage *)GetMsg(Window->UserPort))
  189.         {    ULONG        class = message->Class;
  190.  
  191.             ReplyMsg(&message->ExecMessage);
  192.             if (class == GADGETUP) LEAVE;
  193.         }
  194.     }
  195. exitit:
  196.     if (vblank_ok) RemIntServer(INTB_VERTB, &VertBlank);
  197.     if (midi) DeleteMidi(midi);
  198.     if (Window){ ClearMenuStrip(Window); CloseWindow(Window); }
  199.     if (Screen) CloseScreen(Screen);
  200.     if (CamdBase) CloseLibrary(CamdBase);
  201.     if (GfxBase) CloseLibrary(GfxBase);
  202.     if (IntuitionBase) CloseLibrary(IntuitionBase);
  203. }
  204.  
  205. void draw_vu_levels(void)
  206. {    int                    i;
  207.  
  208.     int                    ymin, ymax, y1, y2;
  209.     int                    xmin, xmax;
  210.  
  211.     for (i=0; i<VU_BARCOUNT; i++)
  212.     {
  213.         if (vu_levels[i] > prev_levels[i])
  214.         {
  215.             SetAPen(rp,i/BARS_PER_COLOR+8);
  216.  
  217.             ymin = prev_levels[i];
  218.             ymax = vu_levels[i];
  219.         }
  220.         else
  221.         {    SetAPen(rp,0);
  222.             ymax = prev_levels[i];
  223.             ymin = vu_levels[i];
  224.         }
  225. #if BYNOTE
  226.         xmin = 21 + i * 9;
  227.         xmax = xmin + 7;
  228. #else
  229.         xmin = 21 + i * 18;
  230.         xmax = xmin + 15;
  231. #endif
  232.  
  233.         y1 = 43 - ymax;
  234.         y2 = 43 - ymin - 1;
  235.  
  236.         if (y1 <= y2) RectFill(rp,xmin,y1,xmax,y2);
  237.  
  238.         prev_levels[i] = vu_levels[i];
  239.     }
  240. }
  241.  
  242. int __interrupt __saveds VBlankInterface( void )
  243. {    Signal(myTask,SIGBREAKF_CTRL_F);
  244.     return 0;                                    /* server chain continues        */
  245. }
  246.